package com.wc.AlgoOJ_homework.AlgoOJ_BL3.C_二进制问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/4 10:08
 * @description http://43.138.190.70:8888/p/507?tid=6602a77318e03a818a4bf04e
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 55, M = 66;
    static long[][] f = new long[M][M];
    // 第i位是1的k个1的个数
    static long[][] f1 = new long[M][M];
    static int[] num = new int[M];
    static int numLen = 0;
    static int k;
    static long n;

    static void init() {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0) f[i][j] = 1;
                else f[i][j] = f[i - 1][j] + f[i - 1][j - 1];
            }
        }
        for (int i = 1; i < M; i++) {
            for (int j = 1; j <= i; j++) {
                f1[i][j] = f[i - 1][j - 1];
            }
        }
    }

    public static void main(String[] args) {
        init();
        n = sc.nextLong();
        k = sc.nextInt();
//        cal(n, k);
        while (n > 0) {
            num[++numLen] = (int) (n & 1);
            n >>= 1;
        }
        long res = 0;
        int last = 0;
        for (int i = numLen; i >= 1; i--) {
            if (num[i] == 1 && i != numLen) {
                if (i - 1 > 0 && i - 1 >= k - last)
                    res += f[i - 1][k - last];
            }
            last += num[i];
            if (last == k) {
                res++;
                break;
            }
        }
        for (int j = 1; j < numLen; j++) {
            res += f1[j][k];
        }

        out.println(res);
        out.flush();
    }

    static void cal(long n, int m) {
        long res = 0;
        for (long i = 1; i <= n; i++) {
            int cnt = 0;
            for (int j = 0; j <= 63; j++) {
                if ((i >> j & 1) == 1)
                    cnt++;
            }
            if (cnt == m) res++;
        }
        System.out.println(res);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
